//
//  SetUpVC.m
//  u76ho
//
//  Created by m-air-01 on 15/12/29.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "SetUpVC.h"
#import "MineFirCell.h"
#import "ModifyPassworkViewController.h"
#import "FeedBackViewController.h"
#import "AboutUsViewController.h"
#import "LoginViewController.h"
#import "HomePageVC.h"
@interface SetUpVC ()
<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
{
    UITableView * _tableView;
    NSMutableArray * dataSource;
}
@end

@implementation SetUpVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

//    AppDelegate * app = [UIApplication sharedApplication].delegate;
//    [app.viewController isHiddenCustomTabBarByBoolean:YES];
//    self.navigationController.navigationBarHidden = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    self.navigationItem.title = @"设置";
    
   // [self addBackItem];
    // Do any additional setup after loading the view.
}
-(void)main
{
    [self.view setBackgroundColor:[UIColor whiteColor]];
    dataSource = [[NSMutableArray alloc]init];
    [dataSource addObjectsFromArray:@[@"消息设置",@"修改密码",@"意见反馈",@"关于我们",@"软件评分"]];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 63, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"MineFirCell" bundle:nil] forCellReuseIdentifier:@"MineFirCell"];
    _tableView.backgroundColor = UIColorFromRGB(0xf1f3f2);
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
}
#pragma mark - _tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 5;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            static NSString * cellID = @"cell";
            UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                UISwitch * switchBtn = [[UISwitch alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
                [switchBtn addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
                [self isPushwithType:@"" andbtn:switchBtn];
                cell.accessoryView = switchBtn;
            }
            cell.textLabel.text = [dataSource objectAtIndex:indexPath.row];
            return cell;
        }
        else
        {
            MineFirCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MineFirCell"];
            cell.contentLab.text = [dataSource objectAtIndex:indexPath.row];
            return cell;
        }

    }
    static NSString * ID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    [cell addSubview:[self createFooterView]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
           
        }
        else if (indexPath.row == 1)
        {
            if ([self isLogin]) {
                ModifyPassworkViewController * modifyPasswordVC = [[ModifyPassworkViewController alloc]init];
                [self.navigationController pushViewController:modifyPasswordVC animated:YES];
            }
        }
        else if (indexPath.row == 2)
        {
            if ([self isLogin]) {
                FeedBackViewController * feedBackVC = [[FeedBackViewController alloc]init];
                [self.navigationController pushViewController:feedBackVC animated:YES];
            }
        }
        else if (indexPath.row == 3)
        {
            AboutUsViewController * aboutUsVC = [[AboutUsViewController alloc]init];
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }
        else
        {
            //1088754003
            NSString *str = [NSString stringWithFormat:
                             @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa     /wa/viewContentsUserReviews?type=Purple+Software&id=%d",
                             1088754003 ];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return SIZETO6H(50);
    }
    else
    {
        return 49;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

-(void)switchChange:(id)sender
{
    if ([self isLogin]) {
        UISwitch * btn = (UISwitch *)sender;
        if (btn.isOn) {
            NSLog(@"开");
            
            if ([self isAllowedNotification]) {
                //允许
                [self isPushwithType:@"yes" andbtn:btn];
            }
            else
            {
                //不允许
                [Singleton alerSendMessage:@"请先允许益企推送"];
                btn.on = NO;
            }
        }
        else
        {
            NSLog(@"关");
            [self isPushwithType:@"no" andbtn:btn];
        }
        NSLog(@"%f",SYSTEM_VERSION);
    }
    
}
-(void)isPushwithType:(NSString *)type andbtn:(UISwitch *)btn
{
    [U76HONet isAllowPushNoticationAction:@"" code:[Singleton share].code isSend:type andDataBlock:^(NSDictionary *dic) {
        NSLog(@"推送设置成功了");
        NSLog(@"%@",dic);
        
//        {
//            errcode = 0;
//            errmsg = no;
//        }
        NSString * isOrno = [dic objectForKey:@"errmsg"];
        if ([isOrno isEqualToString:@"yes"]) {
            btn.on = YES;
        }
        else
        {
            btn.on = NO;
        }
        
    }];
}

-(BOOL)isAllowedNotification
{
    if (SYSTEM_VERSION >= 8.0) {
        UIUserNotificationSettings * setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (UIUserNotificationTypeNone != setting.types) {
            return YES;
        }
    }
    else
    {
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (UIRemoteNotificationTypeNone != type) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - createFooterView
-(UIView *)createFooterView
{
    UIView * footerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SIZETO6H(50))];
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(SCREEN_WIDTH/2-50, footerV.height/2-15, 100, 30);
    [backBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(quikAction:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    backBtn.layer.masksToBounds = YES;
    backBtn.layer.borderColor = [[UIColor redColor] CGColor];
    backBtn.layer.borderWidth = 1.0;
    [footerV addSubview:backBtn];
    return footerV;
    
}

#pragma mark -- 退出登录
-(void)quikAction:(UIButton *)btn
{
    [Singleton share].code = nil;
    [Singleton share].mobile = nil;
    [Singleton share].yqcode = nil;
    [Singleton share].userInformation = nil;
    [Singleton share].token = nil;
    [[Singleton share] saveUserInformation];
    [Singleton alerSendMessage:@"退出登录"];
    _logoutBlock();
    [self performSelector:@selector(homeback) withObject:nil afterDelay:1.0];
}
-(void)homeback
{
//    AppDelegate * app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    app.window.rootViewController = app.viewController;
    
    LoginViewController * loginVC = [[LoginViewController alloc]init];
    loginVC.isHome = YES;
    
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
    
    nav.navigationBarHidden = YES;
    
    [self presentViewController:nav animated:YES completion:nil];
    //[self.navigationController pushViewController:loginVC animated:NO];
//    for (UIViewController * vc in self.navigationController.viewControllers) {
//        if ([vc isKindOfClass:[HomePageVC class]]) {
//            [self.navigationController popToViewController:vc animated:YES];
//        }
//    }
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                [self presentViewController:nvc animated:YES completion:nil];
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        [self presentViewController:nvc animated:YES completion:nil];
    }
}

#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
